# -*- coding: latin-1 -*-

import threading, logging

class Queue(threading.Thread):
    def __init__(self, bot, player_id):
        logging.info("Creating queue for %d",player_id)
        threading.Thread.__init__(self)
        
        self.__bot = bot
        self.__player_id = player_id
        
    def __del__(self):
        logging.info("Queue (%d) stopped.",self.__player_id)
        self.stop()
        
    def add(self, tech_id, planet_id, count):
        logging.info("Adding item to queue (player: %d, id: %d, planet: %d, count: %d)",self.__player_id,tech_id,planet_id,count)
        
        self.__bot.cursor.execute("INSERT INTO queue (player_id, tech_id, planet_id, count) VALUES (%d, %d, %d, %d)" % (self.__player_id,tech_id,planet_id,count))
        self.__bot.db.commit()
        
    def rem(self, num):
        logging.info("Removing item #%d from queue (player %d)",num,self.__player_id)
        
        self.__bot.cursor.execute("DELETE FROM queue WHERE id = %d AND player_id = %d" % (num, self.__player_id))
        self.__bot.db.commit()
        
    def get(self, planet_id):
        logging.info("Getting queue for player %d",self.__player_id)
        
        self.__bot.cursor.execute("SELECT id, tech_id, count FROM queue WHERE player_id = %d AND planet_id = %d" % (self.__player_id, planet_id))
        return self.__bot.cursor.fetchall()